package com.cp.helper.dao;

import com.alibaba.cobar.client.CobarSqlMapClientDaoSupport;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述: 基础Dao实现
 * @author chenpeng
 * @date 2013-5-16 
 */
public class BaseDaoSupport<T extends Serializable> extends CobarSqlMapClientDaoSupport implements BaseDao<T>
{
    @Resource(name = "sqlMapClient")
    private SqlMapClient sqlMapClient;

    @PostConstruct
    public void initSqlMapClient() {
        super.setSqlMapClient(sqlMapClient);
    }

	//类名
	private String className;

	public SqlMapClientTemplate getBaseSqlMapDao()
	{
		return getSqlMapClientTemplate();
	}

	@SuppressWarnings("unchecked")
	public T selectByKey(Integer paramId) throws Exception{
		StringBuffer stringBuffer = new StringBuffer(className);
	    stringBuffer.append(".select");
	    stringBuffer.append(className);
	    stringBuffer.append("ByKey");
		return (T) getBaseSqlMapDao().queryForObject(stringBuffer.toString(),paramId);
	}

	@SuppressWarnings("unchecked")
	public List<T> selectList(Map<String, Object> paramQryMap) throws Exception{
		StringBuffer stringBuffer = new StringBuffer(className);
	    stringBuffer.append(".select");
	    stringBuffer.append(className);
	    stringBuffer.append("List");
		return (List<T>) getBaseSqlMapDao().queryForList(stringBuffer.toString(),paramQryMap);
	}
	

	public Integer selectTotal(Map<String, Object> paramQryMap) throws Exception{
		StringBuffer stringBuffer = new StringBuffer(className);
	    stringBuffer.append(".select");
	    stringBuffer.append(className);
	    stringBuffer.append("Total");
		return (Integer) getBaseSqlMapDao().queryForObject(stringBuffer.toString(),paramQryMap);
	} 
	

	public Integer insert(T paramObject) throws Exception
	{
		StringBuffer stringBuffer = new StringBuffer(className);
	    stringBuffer.append(".insert");
	    stringBuffer.append(className);
		return (Integer) getBaseSqlMapDao().insert(stringBuffer.toString(),paramObject);
 	}
	

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<BigDecimal> insertBatch(final List<T> paramTList) throws Exception
	{
		final StringBuffer stringBuffer = new StringBuffer(className);
	    stringBuffer.append(".insert");
	    stringBuffer.append(className);
	    return (List<BigDecimal>) getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor executor)	throws SQLException	{
				List<BigDecimal> objIdList = new ArrayList<BigDecimal>();
				if(paramTList!=null){
					executor.startBatch();
					for (T obj : paramTList){
						objIdList.add((BigDecimal) getBaseSqlMapDao().insert(stringBuffer.toString(),obj));
					}
					executor.executeBatch();
				}
				return objIdList;
			}
		});
	}
	

	public int update(T paramObject) throws Exception
	{
		StringBuffer stringBuffer = new StringBuffer(className);
	    stringBuffer.append(".update");
	    stringBuffer.append(className);
		stringBuffer.append("ByKey");
		return getBaseSqlMapDao().update(stringBuffer.toString(),paramObject);
 	}
	

	public int delete(Integer paramId) throws Exception
	{
		StringBuffer stringBuffer = new StringBuffer(className);
	    stringBuffer.append(".delete");
	    stringBuffer.append(className);
		stringBuffer.append("ByKey");
		return getBaseSqlMapDao().delete(stringBuffer.toString(),paramId);
 	}


	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int deleteBatch(final List<Integer> paramIdList) throws Exception
	{
		final StringBuffer stringBuffer = new StringBuffer(className);
	    stringBuffer.append(".delete");
	    stringBuffer.append(className);
	    
		return (Integer)getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor executor)	throws SQLException	{
				int deleteCount = 0;
				if(paramIdList!=null){
					executor.startBatch();
					for (Integer id : paramIdList){
						deleteCount = deleteCount + getBaseSqlMapDao().delete(stringBuffer.toString(),id);
					}
					executor.executeBatch();
				}
				return deleteCount;
			}
		});
	}

	public void setClassName(String className)
	{
		this.className = className;
	}
}
